package com.example.warehouse.contrallor;

import com.example.warehouse.entity.*;
import com.example.warehouse.page.Page1;
import com.example.warehouse.service.*;
import com.example.warehouse.utils.CurrentUser;
import com.example.warehouse.utils.TokenUtils;
import com.example.warehouse.utils.WarehouseConstants;
import jakarta.websocket.server.PathParam;
import lombok.Data;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.management.Query;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class contrallorProduct {
    @Autowired
    private ProductService productService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private PaceService paceService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private ProductTypeService productTypeService;
    @RequestMapping("/product-page-list")
    public Result PageProduct(Page1 page1, Product product){
        page1 = productService.pageGet(page1, product);
        return Result.ok(page1);
    }

    @RequestMapping("/store-list")
    public Result findAllStore(){
        List<Store> allStore = storeService.findAllStore();
        return Result.ok(allStore);
    }

    @RequestMapping("/brand-list")
    public Result findAllBrand(){
        List<Brand> allBrand = brandService.findAllBrand();
        return Result.ok(allBrand);
    }

    @RequestMapping("/supply-list")
    public Result findAllSupply(){
        List<Supply> allSupply = supplyService.findAllSupply();
        return Result.ok(allSupply);
    }

    @RequestMapping("/place-list")
    public Result findAllPlace(){
        List<Place> allPlace = paceService.findAllPlace();
        return Result.ok(allPlace);
    }

    @RequestMapping("/unit-list")
    public Result findAllUnit(){
        List<Unit> allunit = unitService.findAllunit();
        return Result.ok(allunit);
    }

    @RequestMapping("/category-tree")
    public Result categoryTree(){
        List<ProductType> typeTree = productTypeService.findTypeTree();
        return Result.ok(typeTree);
    }

    @Value("${file.upload-path}")
    private String uploadPath;
    @RequestMapping("/img-upload")
    public Result uploadImage(MultipartFile file) throws Exception {
        File file1 = ResourceUtils.getFile(uploadPath);
        String file1AbsolutePath = file1.getAbsolutePath();
        String originalFilename = file.getOriginalFilename();
        String uploadFilePath=file1AbsolutePath+"\\"+originalFilename;
        file.transferTo(new File(uploadFilePath));
        return Result.ok("图片上传成功");
    }
    @Autowired
    private TokenUtils tokenUtils;
    @PostMapping("/product-add")
    public Result addProduct(@RequestBody Product product,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        product.setCreateBy(currentUser.getUserId());
        product.setCreateTime(new Date());
        product.setUpDownState("1");
        productService.insertProduct(product);
        return Result.ok("新增产品成功");
    }


    @PutMapping("/state-change")
    public Result updateState(@RequestBody Product product){
        productService.updateState(product);
        return Result.ok("操作成功");
    }


    @PutMapping("/product-update")
    public Result updateProduct(@RequestBody Product product,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        product.setUpdateTime(new Date());
        product.setUpdateBy(currentUser.getUserId());
        productService.updateProduct(product);
        return Result.ok("更改成功");
    }

    @DeleteMapping("/product-list-delete")
    public Result deleteList(@RequestBody List<Integer> deleteList){
        productService.deleteList(deleteList);
        return Result.ok("批量删除成功");
    }


}
